$(window).load(function() {
	var personPhoto;
	var options =
	{
		thumbBox: '.thumbBox',
		spinner: '.spinner',
		imgSrc: ''
	}
	var cropper = $('.imageBox').cropbox(options);
	$('#upload-file').on('change', function(){
		var reader = new FileReader();
		reader.onload = function(e) {
			options.imgSrc = e.target.result;
			cropper = $('.imageBox').cropbox(options);
			lazycut();
		}
		reader.readAsDataURL(this.files[0]);
		$(this).files = [];
	})
	var cut = function(){
		var img = cropper.getDataURL();
		personPhoto = cropper.getDataURL();
		$('.cropped').html('');
		$('.cropped').append('<img src="'+img+'" align="absmiddle" style="width:64px;margin-top:4px;border-radius:64px;box-shadow:0px 0px 12px #7E7E7E;" ><p>64px*64px</p>');
		$('.cropped').append('<img src="'+img+'" align="absmiddle" style="width:128px;margin-top:4px;border-radius:128px;box-shadow:0px 0px 12px #7E7E7E;"><p>128px*128px</p>');
		$('.cropped').append('<img src="'+img+'" align="absmiddle" style="width:180px;margin-top:4px;border-radius:180px;box-shadow:0px 0px 12px #7E7E7E;"><p>180px*180px</p>');		
	}
	var lazycut = function() {
		setTimeout(function() {
			cut();
		},100)
	}
	$('#btnZoomIn').on('click', function(){
		cropper.zoomIn();
		lazycut();
	})
	$('#btnZoomOut').on('click', function(){
		cropper.zoomOut();
		lazycut();
	})
	$("#close").click(function(){
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index); 
	})
	$(".imageBox").bind('mousewheel DOMMouseScroll mouseup', function(){
		lazycut();
	});
	var upload = function(data){
		 $.post("userWeb/updateheadpicture",{
			 headimg:data
				    },function(data){
				       if(data.code=="0"){
				    	var index = parent.layer.getFrameIndex(window.name);
				    	parent.location.reload();  
				       } 
				    });
	}
	$("#upload").click(function() {
		if (personPhoto==undefined||personPhoto=="") {
			layer.msg('请选择头像');
		}else{
			personPhoto = personPhoto.split(",")[1];
			upload(personPhoto);
		}
	})
});